package cn.gailvlun.gll.presentation.hotel.day

import android.app.Activity
import android.content.Intent
import android.databinding.DataBindingUtil
import android.os.Bundle
import android.support.v4.view.ViewPager
import android.util.Log
import android.widget.TextView
import cn.gailvlun.gll.R
import cn.gailvlun.gll.databinding.ActivityHotelTemplateBinding
import cn.gailvlun.gll.net.HotelTemplate
import cn.gailvlun.gll.presentation.base.BaseActivity
import cn.gailvlun.gll.util.ToastUtil
import io.reactivex.Observable
import rx_activity_result2.Result
import rx_activity_result2.RxActivityResult


class HotelTemplateActivity : BaseActivity() {

    companion object {
        fun openActivity(context: Activity): Observable<Result<Activity>> {
            val intent = Intent(context, HotelTemplateActivity::class.java)
            return RxActivityResult.on(context)
                    .startIntent(intent)
        }
    }

    private lateinit var mBinding: ActivityHotelTemplateBinding
    private lateinit var pagerAdapter: HotelTemplatePagerAdapter

    override fun initComponent() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_hotel_template)

        val titles = resources.getStringArray(R.array.hotel_template_tab_title)

        pagerAdapter = HotelTemplatePagerAdapter(supportFragmentManager)
        mBinding.vp.adapter = pagerAdapter

        mBinding.stl.setCustomTabView { container, position, adapter ->
            val view = layoutInflater.inflate(R.layout.tab_hotel_template, container, false)
            val tv: TextView = view.findViewById(R.id.tv)
            tv.text = titles[position]
            when (position) {
                0 -> {
                    tv.setTextColor(mContext.resources.getColor(R.color.greyishBrown))
                }
                1 -> {
                    tv.setTextColor(mContext.resources.getColor(R.color.pinkishGrey))
                }
            }
            return@setCustomTabView view
        }

        mBinding.vp.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {
            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
            }

            override fun onPageSelected(position: Int) {
                for (i in titles.indices) {
                    val view = mBinding.stl.getTabAt(i)
                    Log.e("tag", "i: " + i)
                    val tv = view.findViewById<TextView>(R.id.tv)
                    if (i == position) {
                        tv.setTextColor(resources.getColor(R.color.greyishBrown))
                    } else {
                        tv.setTextColor(resources.getColor(R.color.pinkishGrey))
                    }
                }
            }
        })
        mBinding.stl.setViewPager(mBinding.vp)
    }

    override fun createHandler() {
        mBinding.nbv.setOnMenuClickListener {
            var template = getCurrentTemplate()
            if (template == null) {
                ToastUtil.showShort(getString(R.string.hotel_template_select_warning))
                return@setOnMenuClickListener
            }

            setResult(Activity.RESULT_OK, Intent().apply {
                putExtra("template", template)
            })
            finish()
        }

        mBinding.nbv.setOnBackListener {
            onBackPressed()
        }
    }

    fun getCurrentTemplate(): HotelTemplate? {
        val currentFragment = pagerAdapter.getCurrentFragment()
        return currentFragment.getCurrentTemplate()
    }

    override fun loadData(savedInstanceState: Bundle?) {

    }

}